package com.broadengate.view.model;


public class BaseModel
{
    
    /**
     * 当前页
     */
    private int page = 0;
    
    /**
     * easyui传递的页大小
     */
    
    private int rows = 20;
    
    /**
     * 总行数
     */
    private int rowCount = 0; //
    
    /**
     * 总页数
     */
    private int pageCount = 0; // 总页数
    
    /**
     * 当前页起始记录
     */
    private int pageOffset = 0;//
    
    /**
     * 当前页到达的记录
     */
    private int pageTail = 0;//
    
    /**
     * 排序
     */
    private String orderby;
    
    /**
     * 是否排序，默认为true
     */
    private boolean orderDirection = true;
    
    protected void doPage()
    {
        this.pageCount = this.rowCount / this.rows + 1;
        // 如果模板==0，且总数大于1，则减一
        if ((this.rowCount % this.rows == 0) && pageCount > 1)
            this.pageCount--;
        //避免页面传送的时候第一次传入的为0
        if (this.page == 0)
        {
            this.page = 1;
        }
        this.pageOffset = (this.page - 1) * this.rows;
        this.pageTail = this.pageOffset + this.rows;
        if ((this.pageOffset + this.rows) > this.rowCount)
            this.pageTail = this.rowCount;
    }
    
    public String getOrderCondition()
    {
        String condition = "";
        if (this.orderby != null && this.orderby.length() != 0)
        {
            condition = " order by " + orderby;
        }
        return condition;
    }
    
    public String getMysqlQueryCondition()
    {
        String condition = "";
        condition = " limit " + pageOffset + "," + rows;
        return condition;
    }
    
    public void setOrderDirection(boolean orderDirection)
    {
        this.orderDirection = orderDirection;
    }
    
    public boolean isOrderDirection()
    {
        return orderDirection;
    }
    
    public void setPageCount(int pageCount)
    {
        this.pageCount = pageCount;
    }
    
    public int getPageCount()
    {
        return pageCount;
    }
    
    public int getPage()
    {
        return page;
    }
    
    public void setPage(int page)
    {
        this.page = page;
    }
    
    public void setPageOffset(int pageOffset)
    {
        this.pageOffset = pageOffset;
    }
    
    public int getPageOffset()
    {
        return pageOffset;
    }
    
    public int getRows()
    {
        return rows;
    }
    
    public void setRows(int rows)
    {
        this.rows = rows;
    }
    
    public void setPageTail(int pageTail)
    {
        this.pageTail = pageTail;
    }
    
    public int getPageTail()
    {
        return pageTail;
    }
    
    public void setRowCount(int rowCount)
    {
        this.rowCount = rowCount;
        this.doPage();
    }
    
    public int getRowCount()
    {
        return rowCount;
    }
    
    public String getOrderby()
    {
        return orderby;
    }
    
    public void setOrderby(String orderby)
    {
        this.orderby = orderby;
    }
}
